package edu.cdnu.service.impl;

import java.util.List;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import edu.cdnu.dao.TeacherDao;
import edu.cdnu.pojo.TeacherPojo;
import edu.cdnu.service.TeacherService;

@Service
public class TeacherServiceImpl implements TeacherService {

	@Autowired
	private TeacherDao teacherDao;
	
	@Override
	public TeacherPojo selectTeacherById(int id) {
		return teacherDao.selectTeacherById(id);
	}

	@Override
	public List<TeacherPojo> selectTeacherListByIdList(List<Integer> idList) {
		if(idList != null && idList.size() > 0) {
			return teacherDao.selectTeacherListByIdList(idList);
		}else {
			return null;
		}
		
	}
	
	@Override
	public List<TeacherPojo> selectTeacherListByRoleId(Integer roleId) {
		return teacherDao.selectTeacherListByRoleId(roleId);
	}

	@Override
	public void addTeacher(TeacherPojo teacherPojo) {
		// 先判空，这里账号，密码，姓名为必须字段
		if(teacherPojo != null 
				// 这里判空对象是可以用!=null的，后面空字符就需要空字符写在equals前面
				&& teacherPojo.getAccount() != null && !"".equals(teacherPojo.getAccount())
				&& teacherPojo.getName() != null && !"".equals(teacherPojo.getName())
				) {
			// 1.判断密码,为空给默认值
			if(teacherPojo.getPassword() == null || "".equals(teacherPojo.getPassword())) {
				teacherPojo.setPassword("123456");
			}
			// 2.先把密码加密  用账号作盐
			Md5Hash md5=new Md5Hash(teacherPojo.getPassword(),teacherPojo.getAccount());
			teacherPojo.setPassword(md5.toString());
			// 新增加密后的用户信息
			teacherDao.addTeacher(teacherPojo);
		}
	}
	
	@Override
	public void updateTeacher(TeacherPojo teacherPojo) {
		if(teacherPojo != null && teacherPojo.getId() != null) {
			teacherDao.updateTeacher(teacherPojo);
		}
	}

	@Override
	public TeacherPojo selectTeacher(TeacherPojo teacher) {
		// 先判空
		if(teacher==null){
			return null;
		}
		QueryWrapper<TeacherPojo> queryWrapper=new QueryWrapper<>();
		// 姓名
		if(teacher.getName()!=null && !"".equals(teacher.getName())) {
			queryWrapper.eq("name", teacher.getName());
		}
		// id
		if(teacher.getId()!=null) {
			queryWrapper.eq("id", teacher.getId());
		}
		// 账号
		if(teacher.getAccount()!=null && !"".equals(teacher.getAccount())) {
			queryWrapper.eq("account", teacher.getAccount());
		}
		// 角色id
		if(teacher.getRoleId()!=null) {
			queryWrapper.eq("role_id", teacher.getRoleId());
		}
		// 邮箱
		if(teacher.getEmail()!=null && !"".equals(teacher.getEmail())) {
			queryWrapper.eq("email", teacher.getEmail());
		}
		// 电话
		if(teacher.getPhone()!=null && !"".equals(teacher.getPhone())) {
			queryWrapper.eq("phone", teacher.getPhone());
		}
		// 所属院系
		if(teacher.getDepartmentId()!=null) {
			queryWrapper.eq("department_id", teacher.getDepartmentId());
		}
		return teacherDao.selectOne(queryWrapper);
	}

	

}
